/**
 * 8080a emulator
 * triton & ic
 */

#pragma once

/**
 * Portable types definitions.
 */

//#include <inttypes.h>

typedef unsigned short uint16_t;
typedef unsigned char uint8_t;

typedef uint8_t byte;
typedef uint16_t word;

/**
 * Registers pairs macro
 */

#define pair(A, B) \
    union {        \
        word pair; \
        struct { byte A, B; } reg; \
    } A ## B;

typedef struct
{
    pair(A, F)
    pair(B, C)
    pair(D, E)
    pair(H, L)
    word SP, PC, IR;
    byte I, R;
    int cycles;
} EEctx;

/**
 * RAM 65kB (2^16)
 */

const int RAMSIZE = 0x10000;

/**
 * Default register values
 */

const short SP_INIT =  0x2400;
const short F_INIT  =  0x02;

/**
 * Bit flags
 */

enum Flags 
{
	S_FLAG  =  0x80,
	Z_FLAG  =  0x40,
	AC_FLAG =  0x10,
	P_FLAG  =  0x04,
	V_FLAG  =  P_FLAG,
	N_FLAG  =  0x02,
	C_FLAG  =  0x01
};

/**
 * Memory operations
 */

#define MemRead8(add) RAM[add]
#define MemRead16
#define MemRead MemRead8
#define MemWrite(add,val) RAM[add] = val

/**
 * Main CPU class
 */

class EEcpu 
{
public:
	EEcpu();
	~EEcpu();
	void reset();
	void exec(int cycles);

private:
	EEctx* ctx;
	byte* RAM;
};